//
//  LZStaffTextCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/2.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZStaffTextCell.h"

@interface LZStaffTextCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cellNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *cellContentField;
@property (strong, nonatomic) NSIndexPath *indexPTemp;
@end

@implementation LZStaffTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.cellContentField.delegate = self;
}

#pragma mark -
#pragma mark data
- (void)loadCellName:(NSString *)cellName textFieldPlaceholder:(NSString *)placeholderText textFieldText:(NSString *)textFieldText indexPath:(NSIndexPath *)indexPath;
{
    self.cellContentField.enabled = YES;
    self.indexPTemp = indexPath;
    self.cellNameLabel.text = [NSString stringWithFormat:@"%@",cellName];
    self.cellContentField.placeholder = placeholderText;
    if (textFieldText.length != 0) {
        self.cellContentField.text = textFieldText;
    }
    
    if (indexPath.row == 1) {
        self.cellContentField.secureTextEntry = YES;
    }else{
        self.cellContentField.secureTextEntry = NO;
    }
}

- (void)setTextFiledDisabled
{
    self.cellContentField.enabled = YES;
}

#pragma mark -
#pragma mark field delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return !self.isEditStaff;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger totalLength = textField.text.length;
    NSInteger replaceLength = string.length;
    NSInteger changeLength = range.length;

    //字数限制
    NSInteger maxCount;
    if (self.indexPTemp.row == 2) {
        maxCount = 50;
    }else{
        maxCount = 20;
    }
    if (totalLength + replaceLength > maxCount) {
        return NO;
    }

    
    NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
    if (totalLength - changeLength == 0) {
        str = @"";
    }
    if (self.staffTextFieldCellDelegate && [self.staffTextFieldCellDelegate respondsToSelector:@selector(textFieldText:indexPath:)]) {
        [self.staffTextFieldCellDelegate textFieldText:str indexPath:self.indexPTemp];
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.staffTextFieldCellDelegate && [self.staffTextFieldCellDelegate respondsToSelector:@selector(textFieldText:indexPath:)]) {
        [self.staffTextFieldCellDelegate textFieldText:textField.text indexPath:self.indexPTemp];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
